<?php
/**
 * Created by PhpStorm.
 * User: luckystar_D
 * Date: 2017/1/21
 * Time: 上午11:44
 */

namespace api\modules\v1\models;


use linslin\yii2\curl\Curl;
/*
* 广州联卓流量充值接口
*
* 订单状态 $status  001:充值中;
*                  002:充值成功;
*                  003:充值失败;
*                  004:查无此记录;
*                  005:签名错误;
*                  006:参数错误;
*                  007:未设置AIKEY;
*                   -1:无接口访问权限;
*                   1000:请求过于频繁;
*
* 接口状态 $code     000:提交成功;
*                  001:签名错误;
*                  002:未设置APIKEY;
*                  003:余额不足;
*                  004:不存在指定套餐包;
*                  005:参数错误;
*                  100:充值失败;
*                  999:其他错误;
*                   -1:无接口访问权限;
* 接口类型:POST;
* 接口参数:@$param = array(
                'action' => 'createOrder.do',//接口地址(不同接口不同地址)
                'v' => '1.0', //接口版本
                'account' => self::$account, //接口账户
                'mobile' => $post['phone_no'], //手机号码
                'id' => $signparam['id'], //流量包ID(相关数据录入数据库)
                'notifyurl' => self::$backurl,//回调地址
                'sign' => $sign,//签名(根据不同接口不同规则,具体可参考function sign();)
                'othersoId' => $post['out_order_id'],//外部订单号 (唯一标示,防止频繁提交)
            );
*/

class LianZhuo
{
    public static $apikey = 'b720665af5336e1d18510c3c207cfe5b';
    public static $account = 'aimingtian';
    public static $url = 'http://www.llkc8.com/api/flowService.do';
    public static $backurl = 'https://www.tieba8.com/api/web/lianzhuo/site/back';

    public static function submit($post)
    {
        //echo strtoupper(MD5('account=aimingtian&id=GD1008610100&mobile=15220038141&key=b720665af5336e1d18510c3c207cfe5b'));exit;
        $goodsid = Site::GetFlowValNo($post);
        $signparam = array(
            'id' => $goodsid['no'],
            'mobile' => $post['phone_no']
        );
        $sign = self::sign($signparam, 'submit');
        $param = array(
            'action' => 'createOrder.do',
            'v' => '1.0',
            'account' => self::$account,
            'mobile' => $post['phone_no'],
            'id' => $signparam['id'],
            'notifyurl' => self::$backurl,
            'sign' => $sign,
            'othersoId' => $post['out_order_id'],
        );
        $return = self::curl_http(self::$url,$param);
        $return_array = json_decode($return,true);
        $code = !empty($return_array) ? $return_array['code'] : '005';
        $data_array = array(
            'out_order_id' => $post['out_order_id'],
            'flow_val' => $post['flow_val'],
            'phone_no' => $post['phone_no'],
            'order_id' => !empty($return_array['taskID']) ? $return_array['taskID'] : '',
        );
        return self::returncode($code, $data_array);
    }
    //余额查询接口
    public static function blance()
    {
        $post = array();
        $sign = self::sign($post, 'blance');
        $param = array(
            'action' => 'getBalance.do',
            'v' => 1.0,
            'account' => self::$account,
            'sign' => $sign,
        );
        $return = self::curl_http(self::$url,$param);
        $return_array = json_decode($return,true);
        return self::returncode('000', array('blance' => $return_array['balance']));
    }
    //订单查询接口
    public static function query($post)
    {
        $sign = self::sign($post, 'query');
        $param = array(
            'action' => 'queryOrder.do',
            'v' => 1.0,
            'account' => self::$account,
            'taskid' => $post['out_order_id'],
            'sign' => $sign,
        );
        $return = self::curl_http(self::$url, $param);
        $return_array = json_decode($return, true);

        return self::querycode($return_array['code']);
    }
    //回调地址接口
    public function back()
    {
        /*
         *
         */
    }
    //接口sign验证规则
    public static function sign($param, $type){
        $string = '';
        if ($type == 'submit') {
            $string .= 'account=' . self::$account . '&id=' . $param['id'] . '&mobile=' . $param['mobile'] . '&notifyurl=' . self::$backurl;
        } elseif ($type == 'blance') {
             $string .= 'account=' . self::$account;
        } elseif ($type == 'query') {
            $string .= 'account=' . self::$account . '&taskid=' . $param['out_order_id'];
        }
        $sign = strtoupper(MD5($string . '&key=' . self::$apikey));
        return $sign;
    }
    //curl_http
    public static function curl_http($url,$param){
        $curl = new Curl();
        $response = $curl->setOption(
            CURLOPT_POSTFIELDS,
            http_build_query($param)
        )->post($url);
        return $response;
    }
    public static function querycode($code = '001', $data = array())
    {
        switch ($code)
        {
            case '001' :
                $returncode = 2;
                $message = '充值中';
                break;
            case '002' :
                $returncode = 3;
                $message = '充值成功';
                break;
            case '003' :
                $returncode = 4;
                $message = '充值失败';
                break;
            default :
                $returncode = 1;
                $message = '待充值';
                break;
        }
        return json_encode([
            'code' => $returncode,
            'message' => $message,
            'data' => $data
        ], JSON_UNESCAPED_UNICODE);
        //Yii::$app->end();
    }
    public static function returncode($code = '005', $data = array())
    {
        switch ($code)
        {
            case '000' :
                $returncode = 0;
                $message = '处理成功';
                break;
            case '001' :
                $returncode = -3;
                $message = '鉴权错误';
                break;
            case '002' :
                $returncode = -3;
                $message = '鉴权错误';
                break;
            case '003' :
                $returncode = -6;
                $message = '接口账户余额不足';
                break;
            case '004' :
                $returncode = -8;
                $message = '通道异常';
                break;
            case '005' :
                $returncode = -1;
                $message = '参数错误';
                break;
            case '100' :
                $returncode = -7;
                $message = '系统异常';
                break;
            case '999' :
                $returncode = -7;
                $message = '系统异常';
                break;
            case -1 :
                $returncode = -7;
                $message = '系统异常';
                break;
            default :
                $returncode = 0;
                $message = '处理成功';
                break;
        }
        return json_encode([
            'code' => $returncode,
            'message' => $message,
            'data' => $data
        ], JSON_UNESCAPED_UNICODE);
        //Yii::$app->end();
    }

}